package com.lydms.interval;

import com.lydms.pojo.Page;
import com.lydms.service.LearnSmsService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling
public class LearnSmsIterval {

    private final static Logger logger = LogManager.getLogger(LearnSmsIterval.class);

    @Autowired
    private LearnSmsService learnSmsService;

    @Value(value = "${learnSms.phones}")
    private String phones;

    /**
     * 定时时间1，发送邮件
     */
    @Scheduled(cron = "${learnSms.interval}")
    private void sendEmail() {
        timeLog();
        List<Page> pageList = learnSmsService.getPage();
        List list = learnSmsService.sendSms(pageList, phones);
        if (list!=null){
        logger.info("手机号"+phones+"发送短信成功");
        }else {
            logger.info("手机号"+phones+"发送短信失败");
        }
    }

    /**
     * 打印当前时间
     */
    private void timeLog() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String format1 = format.format(date);
        logger.info(format1 + " 发送短信的手机号为:{}", phones);
    }
}
